package com.landmark;

import java.awt.EventQueue;
import java.io.FileWriter;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.*;
import java.util.Map.Entry;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * This does not work as the site does not allow for getting the links, as it is
 * using the session id attached to link when clicked so have to find some other
 * way to get the links using the session
 *
 * @author Landmark IT
 */
public class ChefWorksCat_Test extends SwingWorker<Boolean, Boolean> {

    FileWriter fw = null;
    FileWriter errorFile = null;
    int pageNo;
    StringBuffer sb = new StringBuffer();
    Hashtable<String, String> htModelLinks = new Hashtable<String, String>();
    Vector<String> vecMenuLinks = new Vector<String>();
    Hashtable<String, String> htSubMenuLinks = new Hashtable<String, String>();
    String status = "";
    JTextArea txtArea;
    UMProgressBar pBar = null;
    int lineCount = 0;
    Map<String, String> cookies = null;

    @Override
    protected Boolean doInBackground() throws Exception {
        //start();
        test();

        return true;
    }

    @Override
    protected void done() {
        Util.taskCompleteMsg(status);
    }

    private void test() throws Exception {
        
//        Connection.Response resCookie = Jsoup.connect("http://www.bhtelecom.ba/imenik_telefon.html")
//            .method(Method.GET)
//            .execute();
//    String sessionId = resCookie.cookie("PHPSESSID");
//        System.out.println(sessionId);
//        String url = "http://www.chefworks.com/?login=1&page=account/index";
////        File file = new File("/path/to/file.ext");
//
//        MultipartEntity entity = new MultipartEntity();
//        entity.addPart("user", new StringBody("kirankumar1999"));
//        entity.addPart("password", new StringBody("kelkum2003"));
////        entity.addPart("email", new StringBody("info@tutorialswindow.com"));
////        entity.addPart("file", new InputStreamBody(new FileInputStream(file), file.getName()));
//
//        HttpPost post = new HttpPost(url);
//        post.setEntity(entity);
//
//        HttpClient client = new DefaultHttpClient();
//        HttpResponse response = client.execute(post);
//        String html = EntityUtils.toString(response.getEntity());
////        System.out.println(html);
//        Document document = Jsoup.parse(html, url);

//        HttpContext localContext = new BasicHttpContext(); 
//
// // Create a local instance of cookie store
//CookieStore cookieStore = new BasicCookieStore();
//
//// Bind custom cookie store to the local context
//localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//
//// execute the post within the context
//
//MultipartEntity entity = new MultipartEntity();
//entity.addPart("username", new StringBody("kirankumar1999"));
//entity.addPart("password", new StringBody("kelkum2003"));
//
//HttpPost post = new HttpPost("http://www.chefworks.com/?login=1&page=account/index");
//post.setEntity(entity);
//
//HttpClient client = new DefaultHttpClient();
////HttpResponse response = client.execute(post);
//HttpResponse response = client.execute(post,localContext);
//
//String html = EntityUtils.toString(response.getEntity());
//
//Document document = Jsoup.parse(html, "http://www.chefworks.com/?page=shop/dev201104_browse_aprons_collection&category=aprons");
//
//        System.out.println(document);
//        
//        
        Response res1 = Jsoup.connect("http://www.chefworks.com/").method(Method.POST).execute();
        Response res2 = Jsoup.connect("http://www.chefworks.com/?page=shop/dev201104_browse_coats_collection&category=coats").method(Method.GET).execute();
        Document docs = res2.parse();
        Response res3 = Jsoup.connect("http://www.chefworks.com/?page=shop/dev200908_browse_coolvent_collection&category=coolvent&PHPSESSID=n86u5chjpro4fucollbepud604")
                .method(Method.GET).execute();
        String sessionId = res3.cookie("PHPSESSID");
        System.out.println(sessionId);
//This will get you cookies
        Map<String, String> cookies = res3.cookies();
        System.out.println(cookies.size());


//And this is the easiest way I've found to remain in session
//Document doc = Jsoup.connect("http://www.chefworks.com/?page=shop/browse_pants_collection_201204&category=pants").cookies(cookies).post();
//        System.out.println(doc);
    }

    public void start() {
        try {
            String dirName = "./outputs";
            String imagesPath = "./outputs/ChefWorksImages";
            String barcoSwatchesPath = "./outputs/ChefWorksSwatches";

            Util.createDirectory(dirName);
            Util.createDirectory(imagesPath);
            Util.createDirectory(barcoSwatchesPath);

            txtArea = Util.createOutputFile("ChefWorks");
            HelperBean helperBean = Util.getHelperBean();

            fw = new FileWriter(helperBean.getOutputFile());
            errorFile = new FileWriter(helperBean.getErrorFile());
            getMenuItems();
            // Gets all the sub categories available in main category
            Enumeration<String> enume = vecMenuLinks.elements();
//            while (enume.hasMoreElements()) {
//                if (Util.getHelperBean().isTerminateCurrentProcess()) {
//                    break;
//                }
//                String str = enume.nextElement();
//                txtArea.setText(str);
//                txtArea.paintImmediately(txtArea.getVisibleRect());
//                getChildCatsFromMainCats(str);
//            }
//
//            // gets the models 
//            enume = htSubMenuLinks.keys();
//            while (enume.hasMoreElements()) {
//                if (Util.getHelperBean().isTerminateCurrentProcess()) {
//                    break;
//                }
//                String str = enume.nextElement();
//                txtArea.setText(str);
//                txtArea.paintImmediately(txtArea.getVisibleRect());
//                processCategoryPage(str);
//            }
            getCookies();

            txtArea.append("\nFiles are located at : \n" + helperBean.getOutputFile().getAbsolutePath() + "\n" + helperBean.getErrorFile().getAbsolutePath() + "\n");
            txtArea.append("Total products : " + htModelLinks.size() + "\n");
            txtArea.paintImmediately(txtArea.getVisibleRect());
            Util.getHelperBean().setLineCount(htModelLinks.size());

            iterateCollection(htModelLinks);

            if (fw != null) {
                fw.close();
            }

        } catch (Exception e) {
            status = e.toString();
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (Exception e1) {
                // TODO: handle exception
            }
        }
        if (status.trim().equals("")) {
            status = "Done processing.";
        }
        if (Util.getHelperBean().isTerminateCurrentProcess()) {
            status = "Process Terminated";
        }
        txtArea.append("\n" + status);
    }

    /**
     *
     * @param htLinks
     */
    private void iterateCollection(Hashtable<String, String> htLinks) {
        try {
            lineCount = 0;
            pBar = new UMProgressBar();
            Enumeration<String> keys = htLinks.keys();
            fw.write("Category_Structure, Product_Name, Description, Product_Image_URL\n");
            while (keys.hasMoreElements()) {
                if (Util.getHelperBean().isTerminateCurrentProcess()) {
                    break;
                }
                String key = (String) keys.nextElement();
                txtArea.append("Processing page: " + key + "\n");
                txtArea.paintImmediately(txtArea.getVisibleRect());
                sb = new StringBuffer();
                Util.getHelperBean().setCurrentCount(lineCount++);

                processModels(htLinks.get(key).toString());
                fw.write(sb.toString().replaceAll("\n", "").replace("   ", "").replaceAll("<#NEWLINE#>", "\n"));
                fw.flush();
                if (Util.getHelperBean().getLineCount() == lineCount) {
                    Util.getHelperBean().setProcessingDone(true);
                    pBar.close();
                }
                EventQueue.invokeLater(pBar);
            }
        } catch (Exception e) {
        }
    }

    /**
     *
     * @param aUrl
     */
    private void processModels(String aUrl) {
        try {
            Parser parser = Util.getParserFromUrl(aUrl, null);

            NodeList nl = parser.parse(null);
            String catStructure = aUrl.substring(aUrl.indexOf("&category=") + 10, aUrl.lastIndexOf("&"));
            String prdtNameLs = Util.getHelperBean().getInputHTML().select(".prod_desc").text();

            String prdtDescLs = Util.getHelperBean().getInputHTML().select(".desc_text").text();
            prdtDescLs += Util.getHelperBean().getInputHTML().select(".desc_bullets").text();
            Elements prdtImages = Util.getHelperBean().getInputHTML().select("#fp2_1").select("img[src$=.jpg]");
//            Elements prdtSwatches = Util.getHelperBean().getInputHTML().select(".productSwatches").select("li");
//            System.out.println(prdtSwatches);
            // gets all the main images
            sb.append(catStructure.replaceAll(",", "&#44;"));
            sb.append(",");
            sb.append(prdtNameLs.replaceAll(",", "&#44;"));
            sb.append(",");
            sb.append(prdtDescLs.replaceAll(",", "&#44;"));
            sb.append(",");
            for (Element ele : prdtImages) {
                sb.append(ele.absUrl("src"));
                Util.saveImage(ele.absUrl("src"), "./outputs/ChefWorksImages", null);
            }
            sb.append("<#NEWLINE#>");
            // gets all the swatches
//            for (Element ele : prdtSwatches) {                
//                String imageUrl = ele.attr("style").substring(ele.attr("style").indexOf("'")+1,ele.attr("style").lastIndexOf("'"));
//                Util.saveImage(imageUrl, "./outputs/ChefWorksSwatches");
//            }
        } catch (SocketTimeoutException e) {
            try {
                processModels(aUrl);
            } catch (Exception e1) {
            }
        } catch (Exception e2) {
            try {
                errorFile.write(aUrl + " \n" + e2.getMessage() + " \n");
                errorFile.flush();
            } catch (Exception e1) {
            }
        }
    }

//    /**
//     *
//     * @param aNodeList
//     */
//    private void iterateTag(Node aNodeList) {
//        //try{
//        //sb = new StringBuffer();
//        if (aNodeList instanceof TextNode) {
//            // downcast to TextNode
//            TextNode text = (TextNode) aNodeList;
//            // do whatever processing you want with the text
//            //System.out.println (text.getText ());
//            sb.append(text.getText());
//
//        } else if (aNodeList instanceof TagNode) {
//
//            // downcast to TagNode
//            TagNode tag = (TagNode) aNodeList;
//
//            // process recursively (nodes within nodes) via getChildren()				
//            NodeList nl = tag.getChildren();
//            if (null != nl) {
//                try {
//                    for (NodeIterator i = nl.elements(); i.hasMoreNodes();) {
//                        iterateTag(i.nextNode());
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
    private void processCategoryPage(String aUrl) {
        try {
            System.out.println(aUrl.substring(aUrl.indexOf("//") + 2));
            Document doc = Jsoup.connect(aUrl).timeout(100 * 1000).get();
            Elements elements = doc.select("#table6").select("a");
            System.out.println(elements.size());
            for (Element element : elements) {
                String absoluteUrl = element.attr("abs:href");
                htModelLinks.put(absoluteUrl, absoluteUrl);
            }
        } catch (SocketTimeoutException e) {
            try {
                processCategoryPage(aUrl);
            } catch (Exception e1) {
            }
        } catch (Exception e2) {
            try {
                errorFile.write(aUrl + " \n" + e2.getMessage() + " \n");
                errorFile.flush();
            } catch (Exception e1) {
            }
        }
    }

    private void getMenuItems() {
        try {
            Parser parser = Util.getParserFromUrl("http://global.chefworks.com/uniforms/", null);
            Document doc = Util.getHelperBean().getInputHTML();
            Elements elements = doc.select(".hdMenu").select("a[href]");
            System.out.println(elements.size());
            for (Element element : elements) {

                String absoluteUrl = element.attr("abs:href");
                System.out.println(absoluteUrl);
                vecMenuLinks.add(absoluteUrl);
            }
        } catch (SocketTimeoutException e) {
            getMenuItems();
        } catch (Exception e1) {
            try {
                errorFile.write(" \n" + e1.getMessage() + " \n");
                errorFile.flush();
            } catch (Exception e2) {
            }
        }

    }

    private void getCookies() throws IOException {

        Map<String, String> cookies = new HashMap<String, String>();
        Connection.Response conne = Jsoup.connect("http://www.chefworks.com/?login=1&page=account/index").data("username", "kirankumar1999", "password", "kelkum2003").method(Method.POST).execute();

        System.out.println(conne.cookies());
        for (Entry<String, String> cookie : cookies.entrySet()) {
            conne.cookie(cookie.getKey(), cookie.getValue());
        }


//        Document doc  = connection.url("http://www.chefworks.com/?page=shop/dev200908_browse_pants_collection&category=pants").get();
//        System.out.println(doc);
//        System.out.println(        connection.response().cookies().size());
//        Connection.Response res = Jsoup.connect("http://www.chefworks.com/?page=shop/dev200908_browse_pants_collection&category=pants").method(Method.POST).execute();
//        System.out.println(res.cookies().size());
//        for (Entry<String, String> cookie : cookies.entrySet()) {
//            connection.cookie(cookie.getKey(), cookie.getValue());
//            System.out.println("%%%%%%" + cookie.getKey());
//        }
//        Response response = connection.execute();
//        System.out.println(response.cookies().size());
//        Document doc = response.parse();
//        System.out.println(doc);
//        cookies.putAll(response.cookies());
    }

    private void getChildCatsFromMainCats(String str) {
        Connection.Response res = null;
        try {
            Parser parser = Util.getParserFromUrl(str, null);

            Connection.Response resCookie = Jsoup.connect(str).method(Method.POST).execute();
            String sessionId = resCookie.cookie("PHPSESSID");
            System.out.println(sessionId);

            res = Jsoup.connect(str).cookie("PHPSESSID", sessionId).method(Method.POST).execute();

            Document dok = res.parse();


            Document doc = Jsoup.connect(str).cookie("PHPSESSID", sessionId).referrer(str).get();

//    Document doc = res.parse();     

            System.out.println(doc);

//        Document doc = Util.getHelperBean().getInputHTML();

            Elements elements = doc.select("#table5a").select("a");
            System.out.println(elements.size());
            for (Element element : elements) {
                String absoluteUrl = element.attr("abs:href");
                System.out.println(absoluteUrl);
                htSubMenuLinks.put(element.text(), absoluteUrl);
            }
        } catch (SocketTimeoutException e) {
            getChildCatsFromMainCats(str);
        } catch (Exception e1) {
            try {
                errorFile.write(" \n" + e1.getMessage() + " \n");
                errorFile.flush();
                e1.printStackTrace();
            } catch (Exception e2) {
            }
        }
    }
}
